/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.ingsoc2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author sfreiman
 */
public class GeneroDAOHSQL implements GeneroDAO {

    Connection conn;

    /**
     *
     * @param myconn
     */
    public GeneroDAOHSQL(Connection myconn) {
        conn = myconn;
    }

    @Override
    public boolean crear(Genero pp) throws SQLException {
        String query;

        query = "INSERT INTO genero (nombre) VALUES(?)";

        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, pp.getNombre());
        Boolean resultado = (pstmt.executeUpdate() > 0);
        pstmt.close();
        return resultado;

    }

    @Override
    public boolean borrar(Genero pp) throws SQLException {
        Boolean resultado;
        String query;
        PreparedStatement pstst;
        query = "DELETE from genero WHERE id =?";

        pstst = conn.prepareStatement(query);
        pstst.setInt(1, pp.getId());

        resultado = (pstst.executeUpdate() == 1);
        pstst.close();
        return resultado;
    }

    public boolean guardar(Genero pp) throws SQLException {
        String query;
        query = "UPDATE genero SET nombre=? WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, pp.getNombre());
        pstmt.setInt(2, pp.getId());
        Boolean resultado = (pstmt.executeUpdate() == 1);
        pstmt.close();
        return resultado;
    }

    public Genero abrir(int gID) throws SQLException {
        String query;
        ResultSet rst1;
        Genero nGenero;

        query = "SELECT * FROM genero where id=?";
        PreparedStatement stst = conn.prepareStatement(query);
        stst.setInt(1, gID);
        rst1 = stst.executeQuery();
        nGenero = new Genero();
        while (rst1.next()) {
            nGenero.setNombre(rst1.getString("nombre"));
            nGenero.setId(rst1.getInt("id"));
        }
        stst.close();
        rst1.close();

        return nGenero;
    }

    public Collection<Genero> listar() throws SQLException {
        String query;
        Statement stst;
        ResultSet rst1;
        List listaDePeliculas = new ArrayList();

        Genero nGenero;

        query = "SELECT * FROM genero";
        stst = conn.createStatement();
        rst1 = stst.executeQuery(query);
        while (rst1.next()) {
            nGenero = new Genero();
            nGenero.setNombre(rst1.getString("nombre"));
            nGenero.setId(rst1.getInt("id"));

            listaDePeliculas.add(nGenero);
        }
        stst.close();
        rst1.close();
        return listaDePeliculas;
    }

}
